// init.cpp
// Written by, Sohail Qayum Malik[sqm@cyberian.pk]

#include <iostream>
#include "init.h"

int version(void) {

   std::cout<<":)";

   return 0;
}

int help(argument<arg_history>::iterator itB, argument<arg_history>::iterator itE, std::string commands) {

   int ret = 0;
   
   while ( itB != itE ) {
   
      arg_history::value_type foo = *(itB++);
	 	 
	  if (foo.get_arg()) {
	  	    
		 std::cout<<foo.get_arg()<<", "<< commands.substr(foo.get_pos(), foo.get_cmd_size())<<", "<<commands.substr(foo.get_pos() + foo.get_cmt_head_pos(), foo.get_cmt_size())<<std::endl;
	  }
	  else  {
	  
	     std::cout<<"NULL(You did not issue this command), "<< commands.substr(foo.get_pos(), foo.get_cmd_size())<<", "<<commands.substr(foo.get_pos() + foo.get_cmt_head_pos(), foo.get_cmt_size())<<std::endl;
	  } 	  
   }

   return ret;   
}

int init(char const*const*const argv, std::string commands, argument<arg_history>::pointer ptr) throw (args_exception) {

    int ret = 0;

    argument<arg_history>::iterator itB(ptr->begin());
    argument<arg_history>::iterator itE(ptr->end());
	
	arg_history common_arg = itB[COMMON];
	
	if ( itB == itE ) {
	
       std::cout<<"Usage..."<<std::endl;
	   std::cout<<"C:\\>"<<argv[0]<<" < ? | /? | h | -h | help | --help | ... >";
	}	
	else try {
	
	   while ( itB != itE ) {
	   	
	      if ( *itB == HELP ) {
	   
	         argument<arg_history>::iterator a(ptr->begin());
			 argument<arg_history>::iterator b(ptr->end());
	         
			 ret = help(a, b, commands);	   	         
          }
          else if ( *itB == VERSION ) {
	   
	         ret = version();
          }
          else if ( *itB == DOSHEADER ) {
	   
	         ret = dosheader(argv, common_arg, *itB);
          }
          else if ( *itB == IMAGENTHEADERS ) {
		  
		     ret = imagentheaders(argv, common_arg, *itB);
          }
          else if ( *itB == SECTIONS ) {
		  
		     ret = sections(argv, common_arg, *itB);
          }		  
       		
	      itB++;
	   }
    }
    catch(args_exception &e) {
	
	   throw(e); 
    }	

   return ret;
}